package com.wuliu.system.service.applet.impl;

import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wuliu.common.core.domain.entity.BasCust;
import com.wuliu.common.core.domain.entity.SysAppletUser;
import com.wuliu.common.exception.ServiceException;
import com.wuliu.common.utils.DateUtils;

import javax.annotation.Resource;

import com.wuliu.common.utils.SecurityUtils;
import com.wuliu.system.mapper.SysAppletUserMapper;
import com.wuliu.system.service.applet.IBasCustUserService;
import org.springframework.stereotype.Service;
import com.wuliu.system.mapper.BasCustUserMapper;
import com.wuliu.system.domain.BasCustUser;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 客户专属业务员Service业务层处理
 *
 * @author realue
 * @date 2023-11-01
 */
@Service
public class BasCustUserServiceImpl extends ServiceImpl<BasCustUserMapper, BasCustUser> implements IBasCustUserService {
    @Resource
    private BasCustUserMapper basCustUserMapper;
    @Resource
    private SysAppletUserMapper sysAppletUserMapper;

    /**
     * 查询客户专属业务员
     *
     * @param id 客户专属业务员主键
     * @return 客户专属业务员
     */
    @Override
    public BasCustUser selectBasCustUserById(Long id) {
        return basCustUserMapper.selectBasCustUserById(id);
    }

    /**
     * 查询客户专属业务员列表
     *
     * @param basCustUser 客户专属业务员
     * @return 客户专属业务员
     */
    @Override
    public List<BasCustUser> selectBasCustUserList(BasCustUser basCustUser) {
        return basCustUserMapper.selectBasCustUserList(basCustUser);
    }

    /**
     * 新增客户专属业务员
     *
     * @param basCustUser 客户专属业务员
     * @return 结果
     */
    @Override
    public int insertBasCustUser(BasCustUser basCustUser) {
        basCustUser.setCreateTime(DateUtils.getNowDate());
        return basCustUserMapper.insertBasCustUser(basCustUser);
    }

    /**
     * 修改客户专属业务员
     *
     * @param basCustUser 客户专属业务员
     * @return 结果
     */
    @Override
    public int updateBasCustUser(BasCustUser basCustUser) {
        basCustUser.setUpdateTime(DateUtils.getNowDate());
        return basCustUserMapper.updateBasCustUser(basCustUser);
    }

    /**
     * 批量删除客户专属业务员
     *
     * @param ids 需要删除的客户专属业务员主键
     * @return 结果
     */
    @Override
    public int deleteBasCustUserByIds(Long[] ids) {
        return basCustUserMapper.deleteBasCustUserByIds(ids);
    }

    /**
     * 删除客户专属业务员信息
     *
     * @param id 客户专属业务员主键
     * @return 结果
     */
    @Override
    public int deleteBasCustUserById(Long id) {
        return basCustUserMapper.deleteBasCustUserById(id);
    }

    /**
     * 新增专属业务员
     *
     * @param userNo
     * @return
     */
    @Override
    public int insertBasCustUser(String userNo) {
        BasCust cust = SecurityUtils.getBasCust();
        SysAppletUser appletUser = sysAppletUserMapper.selectOne(new LambdaQueryWrapper<SysAppletUser>()
                .eq(SysAppletUser::getSysAccount, cust.getSysAccount())
                .eq(SysAppletUser::getUserNo, userNo));
        if (null == appletUser) {
            throw new ServiceException("业务员不存在");
        }
        long count = basCustUserMapper.selectCount(new LambdaQueryWrapper<BasCustUser>()
                .eq(BasCustUser::getSysAccount, cust.getSysAccount())
                .eq(BasCustUser::getCustId, cust.getId())
                .eq(BasCustUser::getUserNo, userNo));
        if (count > 0) {
            throw new ServiceException("专属业务员重复");
        }
        BasCustUser basCustUser = new BasCustUser();
        basCustUser.setCustId(cust.getId());
        basCustUser.setUserId(appletUser.getUserId());
        basCustUser.setUserName(appletUser.getUserName());
        basCustUser.setUserNo(appletUser.getUserNo());
        basCustUser.setDeptId(appletUser.getDeptId());
        basCustUser.setDeptName(appletUser.getDeptName());
        basCustUser.setDeptNo(appletUser.getDeptNo());
        basCustUser.setSysAccount(cust.getSysAccount());
        basCustUser.setCreateTime(DateUtils.getNowDate());
        basCustUser.setCreateBy(cust.getCustName());
        return basCustUserMapper.insertBasCustUser(basCustUser);
    }
}
